import { prototype } from 'module';
import { defineStore } from 'pinia';
import { parse, stringify } from 'zipson';
import { DateFormatUtil } from '@/util/common_utils';

export const useUserInfoStore = defineStore('userInfo', {
	state: () => {
		return {
			userInfo: {
				userId: '',
				signinTime:null,//服务器记录的登录时间
				lastRefreshTime:null,//最后刷新时间
				isSuperManager: false,
				userToken:{},//认证信息，需要及时更新，否则会过期
				userSignin:{},//用户登录信息
				userDetail: {}//用户详细信息
			}
		};
	},
	actions: {
		setSigninInfo(userSignin, signinTime) {
			if(this.userInfo==null){
				this.userInfo={};
			}
			if(signinTime){
				this.userInfo["signinTime"]=signinTime;
			}else{
				this.userInfo["signinTime"]=DateFormatUtil.formatDateYMDHMS(new Date());
			}
			this.userInfo["lastRefreshTime"]=new Date();
			//拆解OAuth Server返回的UserPrincipal
			/**
			 {
				 "loginName": 1580000000,    //该值是登录页附加的
			     "authorities": [
			         {
			             "authority": "ROLE_read"
			         }
			     ],
			     "details": {
			         "remoteAddress": "127.0.0.1",
			         "sessionId": "604C5BE3AC0C685F6A072B68A428983F"
			     },
			     "authenticated": true,
			     "principal": {
			         "password": null,
			         "username": "lisi",
			         "authorities": [
			             {
			                 "authority": "ROLE_read"
			             }
			         ],
			         "accountNonExpired": true,
			         "accountNonLocked": true,
			         "credentialsNonExpired": true,
			         "enabled": true,
			         "userInDb": {
			             "id": 2,
			             "accountName": "lisi",
			             "phoneNumber": "456",
			             "email": null,
			             "nickname": "李四(222)",
			             "signinPassword": "",
			             "isActive": 0,
			             "userType": 0,
			             "createTime": "2024-07-29 12:09:08",
			             "updateTime": "2024-08-08 20:20:28",
			             "loginTime": null,
			             "loginIp": null,
			             "delFlag": 0,
			             "version": 0
			         }
			     },
			     "credentials": null,
			     "name": "lisi"
			 }
			 */
			this.userInfo["userSignin"]={}
			if(userSignin.authorities){//获取授权
				this.userInfo.userSignin['authorities']=userSignin.authorities;
			}
			if(userSignin.principal&&userSignin.principal.userInDb){//获取用户登录信息
				let _userInDB=userSignin.principal.userInDb;
				_userInDB["loginTime"]=this.userInfo.signinTime;
				_userInDB["loginIp"]=userSignin.details.remoteAddress;
				this.userInfo.userSignin['userInDb']=_userInDB;
				this.userInfo.userSignin['loginName']=userSignin.loginName;
				this.userInfo.userId = _userInDB["id"];
			}
			
		},
		setUserInfo(isSuperManager,userDetail) {
			if(this.userInfo==null){
				this.userInfo={};
			}
			this.userInfo.isSuperManager = isSuperManager;
			this.userInfo.userDetail = userDetail;
		},
		refreshToken(newToken){
			this.userInfo.userToken=newToken;
			this.userInfo.lastRefreshTime=new Date();
		},
		clearUserInfo() {
			if(this.userInfo==null){
				this.userInfo={};
			}
			this.userInfo.userId = '';
			this.userInfo.userToken = '';
			this.userInfo.signinTime=null;
			this.userInfo.lastRefreshTime=null;
			this.userInfo.isSuperManager=false;
			this.userInfo.userSignin ={};
			this.userInfo.userDetail = {};
		}
	},
	persist: {
		enabled: true,
		key: 'my_infor',
		paths: ['userInfo'],
		storage: sessionStorage,
		serializer: {
			deserialize: parse,
			serialize: stringify
		}
	}
});
